<template>
  <div class="app-container">
    <el-form
      :model="formData"
      ref="formData"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="会话编号" prop="dictLabel">
        <el-input
          v-model="formData.sessionId"
          placeholder="请输入会话编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="dictLabel">
        <el-input
          v-model="formData.userTel"
          placeholder="请输入电话"
          clearable
          @keyup.enter.native="handleQuery"
          maxlength="11"
          @input="handleInput"
        />
      </el-form-item>
      <el-form-item label="日期">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['customerService:session:export']"
          >导出</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 表格   -->
    <el-table
      v-loading="loading"
      :data="tableData"
      @selection-change="handleSelectionChange"
    >
    <el-table-column label="序号" align="center" type="index" width="50">
    </el-table-column>
      <el-table-column label="会话编号" align="center" prop="sessionId" />
      <el-table-column
        label="电话"
        align="center"
        :formatter="
          ({ userTel }) => {
            return desensitizePhone(userTel);
          }
        "
        prop="userTel"
      />
      <el-table-column label="坐席编号" align="center" prop="robotId" />
      <el-table-column
        label="状态"
        align="center"
        prop="status"
        :formatter="
          ({ status }) => {
            return status == 0 ? '有效' : status == 1 ? '无效' : status;
          }
        "
      >
      </el-table-column>
      <el-table-column label="消息数量" align="center" prop="msgCount" />

      <el-table-column
        label="通话时长（豪秒）"
        align="center"
        prop="callDuration"
        width="180"
      >
      </el-table-column>
      <el-table-column
        label="会话时间"
        align="center"
        prop="createTime"
      ></el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <router-link
            :to="'/customerService/session-data/index/' + scope.row.id"
            class="link-type"
          >
            <el-button size="mini" type="text" icon="el-icon-view"
              >查看详情</el-button
            >
          </router-link>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="pagination.pageNum"
      :limit.sync="pagination.pageSize"
      @pagination="getList"
    />

    <!-- 消息列表   -->
    <message-list-dialog
      :messageDialogShow="messageDialogShow"
      :sessionId="sessionId"
      @onCloseMessageDialog="onCloseMessageDialog"
    ></message-list-dialog>
  </div>
</template>

<script>
import { sessionList } from "@/api/customerService/session";
import messageListDialog from "./components/messageListDialog.vue";

export default {
  name: "sessionList",
  components: { messageListDialog },
  data() {
    return {
      loading: false,
      tableData: [], //表格数据
      showSearch: true, //显示搜索条件
      // 查询参数
      formData: {
        sessionid: undefined,
        userTel: undefined,
      },
      pagination: {
        pageNum: 1,
        pageSize: 10,
      },
      selectIds: [], //选中的id
      dateRange: [], //日期范围
      total: 0,
      sessionId: "",
      messageDialogShow: false, //消息列表弹窗是否打开
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async getList() {
      let params = {
        ...this.formData,
        ...this.pagination,
      };
      this.loading = true;
      const { total, rows } = await sessionList(
        this.addDateRange(params, this.dateRange)
      );
      this.total = total;
      this.tableData = rows;
      this.loading = false;
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.pagination.pageNum = 1;
      this.getList();
    },

    // 手机号脱敏
    desensitizePhone(phone) {
      if (!phone) {
        return "";
      }
      const reg = /(\d{3})\d{4}(\d{4})/;
      return phone.replace(reg, (match, p1, p2) => `${p1}****${p2}`);
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.formData = {
        sessionid: undefined,
        userTel: undefined,
      };
      this.dateRange = [];
      this.handleQuery();
    },

    handleInput(value) {
      this.formData.userTel = value.replace(/\D/g, "");
    },

    // 查看消息
    handleViewDetail(row) {
      this.messageDialogShow = true;
      this.sessionId = row?.sessionId || "";
    },

    // 关闭消息弹窗
    onCloseMessageDialog() {
      this.messageDialogShow = false;
      this.sessionId = "";
      this.getList();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectIds = selection.map((item) => item.id);
    },

    /** 导出按钮操作 */
    handleExport() {
      const url = `system/sysCustomer/exportMsg`;
      let data = {
        ...this.formData,
      };
      if (this.dateRange?.length) {
        data.params = typeof (data.params) === 'object' && data.params !== null && !Array.isArray(data.params) ? data.params : {};
        data.params['beginTime'] = this.dateRange[0];
        data.params["endTime"] = this.dateRange[1];
      }
      this.download(
        url,
        {
          ...data,
        },
        `role_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style lang="scss" scoped></style>
